package com.example.zbh.dsliao.ui.activty;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.easemob.chat.EMChatManager;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.utils.DliaoUtils;
import com.example.zbh.dsliao.utils.PreferenceHelper;

import de.greenrobot.event.EventBus;

/**
 * Created by zbh on 15/12/2.
 */
public class BackGroundActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout back;
    private ImageView girl_one, girl_two, girl_three, journey_one, journey_two, journey_three, frend,
            handsomeone, handsometwo;
    private String userName;

    @Override
    protected int getLayOut() {
        return R.layout.activity_background;
    }

    @Override
    protected void initView() {
        back = findeView(R.id.back);
        girl_one = findeView(R.id.girl_one);
        girl_two = findeView(R.id.girl_two);
        girl_three = findeView(R.id.girl_three);
        journey_one = findeView(R.id.journey_one);
        journey_two = findeView(R.id.journey_two);
        journey_three = findeView(R.id.journey_three);
        frend = findeView(R.id.frend);
        handsomeone = findeView(R.id.handsomeone);
        handsometwo = findeView(R.id.handsometwo);
    }

    @Override
    protected void initData() {
        EventBus.getDefault().register(this);
        userName = EMChatManager.getInstance().getCurrentUser();
        String backGroundType = PreferenceHelper.getInstance().getCurrentuserBackground(userName);
        if (backGroundType != null){
            switch (backGroundType){
                case Constant.GIRL_ONE:
                    setBackGround(girl_one);
                    break;
                case Constant.GIRL_TWO:
                    setBackGround(girl_two);
                    break;
                case Constant.GIRL_THREE:
                    setBackGround(girl_three);
                    break;
                case Constant.JOURNEY_ONE:
                    setBackGround(journey_one);
                    break;
                case Constant.JOURNEY_TWO:
                    setBackGround(journey_two);
                    break;
                case Constant.JOURNET_THREE:
                    setBackGround(journey_three);
                    break;
                case Constant.FRIEND:
                    setBackGround(frend);
                    break;
                case Constant.BOY_ONE:
                    setBackGround(handsomeone);
                    break;
                case Constant.BOY_TWO:
                    setBackGround(handsometwo);
                    break;
            }
        }
        back.setOnClickListener(this);
        girl_one.setOnClickListener(this);
        girl_two.setOnClickListener(this);
        girl_three.setOnClickListener(this);
        journey_one.setOnClickListener(this);
        journey_two.setOnClickListener(this);
        journey_three.setOnClickListener(this);
        frend.setOnClickListener(this);
        handsomeone.setOnClickListener(this);
        handsometwo.setOnClickListener(this);
    }

    /**
     * 监听主界面的eventbus发过来的的状态
     * (包括 1 == 账号被服务器删除
     * 2 == 账号被别的设备登录)
     * **/
    public void onEventMainThread(Integer data){
        if (data == 2){
            DliaoUtils.showDialogConnection(this);
        }else if (data == 1){
            DliaoUtils.showDialogUserRemoved(this);
        }
    }


    private void setBackGround(ImageView imageView) {
        imageView.setBackgroundResource(R.drawable.dl_icon_biankuang);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.girl_one:
                girl_one.setBackgroundResource(R.drawable.dl_icon_biankuang);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.GIRL_ONE);
                break;
            case R.id.girl_two:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_icon_biankuang);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.GIRL_TWO);

                break;
            case R.id.girl_three:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_icon_biankuang);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.GIRL_THREE);

                break;
            case R.id.journey_one:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_icon_biankuang);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.JOURNEY_ONE);

                break;
            case R.id.journey_two:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_icon_biankuang);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.JOURNEY_TWO);

                break;
            case R.id.journey_three:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_icon_biankuang);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.JOURNET_THREE);

                break;
            case R.id.frend:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_icon_biankuang);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName, Constant.FRIEND);
                break;
            case R.id.handsomeone:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_icon_biankuang);
                handsometwo.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName,Constant.BOY_ONE);
                break;
            case R.id.handsometwo:
                girl_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                girl_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_one.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_two.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                journey_three.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                frend.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsomeone.setBackgroundResource(R.drawable.dl_biankuang_nomal);
                handsometwo.setBackgroundResource(R.drawable.dl_icon_biankuang);
                PreferenceHelper.getInstance().setCurrentuserBackground(userName,Constant.BOY_TWO);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
